package yf.mall.entity;


import lombok.Setter;
import lombok.Getter;
import yf.mall.enums.MallOrderOrderFromEnum;
import yf.mall.enums.MallOrderOrderStatusEnum;
import yf.mall.enums.MallOrderOrderTypeEnum;
import yf.mall.enums.MallOrderPayStatusEnum;

import java.io.Serializable;
import java.util.Date;

import java.math.BigDecimal;
import java.util.List;

/**
 * 商品订单实体
 * 表名 mall_order
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Sun May 24 14:16:14 CST 2020
 */
@Setter
@Getter
public class MallOrderEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    //
    private Long id;
    //单号
    private String orderNo;
    //出货人ID
    private Long fromUserId;
    //ENUM-订单类型:"DELIVERY-发货","TRANSFER-转货"
    private Integer orderType;
    //ENUM-订单来源:"MALL-商城","ADMIN-后台","COD-补货"
    private Integer orderFrom;
    //ENUM-转货业务类型:"BUY-进货","SEND-调配","BACK-退回","RETURN-还货","AUTO_BUY-自动进货"
    private Integer busType;
    //ENUM-支付方式:"CASH-进货","WX-微信"
    private Integer payWay;
    //用户ID
    private Long userId;
    //总价
    private BigDecimal price;
    //总数量
    private Integer quantity;
    //下单时间
    private Date orderTime;
    //起止时间
    Date[] orderTimes;
    //支付时间
    private Date payTime;
    //起止时间
    Date[] payTimes;
    //发货时间
    private Date deliverTime;
    //起止时间
    Date[] deliverTimes;
    //确认收货时间
    private Date confirmTime;
    //起止时间
    Date[] confirmTimes;
    //退货时间
    private Date backTime;
    //起止时间
    Date[] backTimes;
    //退款时间
    private Date refundTime;
    //起止时间
    Date[] refundTimes;
    //拒绝时间
    private Date refuseTime;
    //起止时间
    Date[] refuseTimes;
    //ENUM-发货订单状态:"NEW-新订单","DELIVERING-配货中","DELIVERED-已发货","RECEIVED-已收货","REFUNDING-待退款","REFUNDED-已退款","REFUSE-已拒绝","DONE-已完成","CANCELED-已取消"
    private Integer orderStatus;
    //ENUM-支付状态:"NOT_PAID-未支付","PAID-已支付"
    private Integer payStatus;
    //ENUM-是否结束:"CLOSED-已结束","OPEN-未结束"
    private Boolean closed;
    //ENUM-是否自提:"0-否","1-是"
    private Integer mention;
    //物流费
    private BigDecimal logFee;
    //物流公司
    private String logCompany;
    //物流单号
    private String logNo;
    //收货人姓名
    private String userName;
    //收货人手机
    private String mobile;
    //收货地址
    private String address;
    //总积分
    private BigDecimal point;
    //总优惠券
    private BigDecimal coupon;
    //总换购券
    private BigDecimal voucher;
    //总清货券
    private BigDecimal clear;
    //总礼品卷
    private BigDecimal card;
    //备注
    private String remark;
    //取消
    private Boolean cancel;

    //店铺ID
    private Long shopId;

    //订单明细
    List<OrderItemEntity> items;

    private String province;

    private String city;

    private String county;

    private String detail;

    //实际支付价格
//    private BigDecimal payPrice;

    private BigDecimal pay;

    //VIP用户
    private Long vipUserId;

    private String shareCode;

    private String teamCode;

    private Long shareId ;

    private Long teamId;



    public MallOrderEntity() {

    }



    //地址全部地址
    public String getAddress() {
        if(province!=null){
            return province +" " +city+" " + county +" " + detail;
        }else {
            return this.address;
        }

    }

    /**
     * 后台发货订单
     * @param orderNo
     * @param fromUserId
     * @param orderType
     * @param busType
     * @param userId
     * @param price
     * @param quantity
     * @param orderTime
     * @param orderStatus
     * @param logFee
     * @param userName
     * @param mobile
     * @param address
     * @param remark
     */
    public MallOrderEntity(String orderNo, Long fromUserId, Integer orderType, Integer busType, Long userId, BigDecimal price, Integer quantity, Date orderTime, Integer orderStatus, BigDecimal logFee, String userName, String mobile, String province,String city,String county,String detail, String remark,Integer payStatus,Long shopId) {
        this.orderNo = orderNo;
        this.fromUserId = fromUserId;
        this.orderType = orderType;
        this.busType = busType;
        this.userId = userId;
        this.price = price;
        this.quantity = quantity;
        this.orderTime = orderTime;
        this.orderStatus = orderStatus;
        this.logFee = logFee;
        this.userName = userName;
        this.mobile = mobile;
        this.remark = remark;
        this.province =province;
        this.city = city;
        this.county= county;
        this.detail = detail;
        this.payStatus=payStatus;
        this.shopId = shopId;
    }

    /**
     * 后台转货订单 还货订单
     * @param orderNo
     * @param fromUserId
     * @param orderType
     * @param busType
     * @param userId
     * @param price
     * @param quantity
     * @param orderTime
     * @param orderStatus
     * @param remark
     */
    public MallOrderEntity(String orderNo, Long fromUserId, Integer orderType, Integer busType, Long userId, BigDecimal price, Integer quantity, Date orderTime, Integer orderStatus, String remark,Integer payStatus,Long shopId) {
        this.orderNo = orderNo;
        this.fromUserId = fromUserId;
        this.orderType = orderType;
        this.busType = busType;
        this.userId = userId;
        this.price = price;
        this.quantity = quantity;
        this.orderTime = orderTime;
        this.orderStatus = orderStatus;
        this.remark = remark;
        this.payStatus = payStatus;
        this.shopId=shopId;
    }

    /**
     * 商城转货订单
     * @param orderNo
     * @param fromUserId
     * @param orderType
     * @param busType
     * @param payWay
     * @param userId
     * @param price
     * @param quantity
     * @param orderTime
     * @param payTime
     * @param orderStatus
     * @param payStatus
     * @param point
     * @param coupon
     * @param voucher
     * @param remark
     */
    public MallOrderEntity(String orderNo, Long fromUserId, Integer orderType, Integer busType, Integer payWay, Long userId, BigDecimal price, Integer quantity, Date orderTime, Date payTime, Integer orderStatus, Integer payStatus, BigDecimal point, BigDecimal coupon, BigDecimal voucher, String remark,BigDecimal clear,Integer mention,Long shopId) {
        this.orderNo = orderNo;
        this.fromUserId = fromUserId;
        this.orderType = orderType;
        this.busType = busType;
        this.payWay = payWay;
        this.userId = userId;
        this.price = price;
        this.quantity = quantity;
        this.orderTime = orderTime;
        this.payTime = payTime;
        this.orderStatus = orderStatus;
        this.payStatus = payStatus;
        this.point = point;
        this.coupon = coupon;
        this.voucher = voucher;
        this.remark = remark;
        this.clear = clear;
        this.mention = mention;
        this.shopId = shopId;
    }

    /**
     * 商城发货订单
     * @param orderNo
     * @param fromUserId
     * @param orderType
     * @param busType
     * @param payWay
     * @param userId
     * @param price
     * @param quantity
     * @param orderTime
     * @param payTime
     * @param orderStatus
     * @param payStatus
     * @param logFee
     * @param userName
     * @param mobile
     * @param address
     * @param point
     * @param coupon
     * @param voucher
     * @param remark
     */
    public MallOrderEntity(String orderNo, Long fromUserId, Integer orderType, Integer busType, Integer payWay, Long userId, BigDecimal price, Integer quantity, Date orderTime, Date payTime, Integer orderStatus, Integer payStatus, BigDecimal logFee, String userName, String mobile, String province,String city,String county,String detail, BigDecimal point, BigDecimal coupon, BigDecimal voucher, String remark, Integer mention,BigDecimal clear,Long shopId) {
        this.orderNo = orderNo;
        this.fromUserId = fromUserId;
        this.orderType = orderType;
        this.busType = busType;
        this.payWay = payWay;
        this.userId = userId;
        this.price = price;
        this.quantity = quantity;
        this.orderTime = orderTime;
        this.payTime = payTime;
        this.orderStatus = orderStatus;
        this.payStatus = payStatus;
        this.mention = mention;
        this.logFee = logFee;
        this.userName = userName;
        this.mobile = mobile;
        this.province = province;
        this.city = city;
        this.county = county;
        this.detail = detail;
        this.point = point;
        this.coupon = coupon;
        this.voucher = voucher;
        this.remark = remark;
        this.clear = clear;
        this.shopId=shopId;
    }

//    //实际需要付款的金额
//    public BigDecimal getRetailPrice() {
//        BigDecimal retailPrice = price;
//        if (logFee != null) {
//            retailPrice = retailPrice.add(logFee);
//        }
//        if (coupon != null) {
//            retailPrice = retailPrice.subtract(coupon);
//        }
//        return retailPrice;
//    }


    //是否可以取消
    //未支付都可以取消 发货订单 未发货可以取消
    public Boolean getCancel() {
        //已经是取消状态
        if(orderStatus.equals(MallOrderOrderStatusEnum.CANCELED.getIndex())){
            return false;
        }
        //未支付
        if(payStatus!=null&&payStatus.equals(MallOrderPayStatusEnum.NOT_PAID.getIndex())){
            return true;
        }
        //发货订单 还未发货
        if(orderType!=null&&orderType.equals(MallOrderOrderTypeEnum.DELIVERY.getIndex())&&orderStatus!=null&&orderStatus.equals(MallOrderOrderStatusEnum.NEW.getIndex())){
            return true;
        }
        return false;
    }

}
